﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace application
{
    public partial class sysHelpEdit : baseForm 
    {
        public sysHelpEdit()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.helpEd.myOnSave += new commonV2.controls.rtfEditor.OnProcess(DoSaveHelp);
            this.helpEd.myOnExit += new commonV2.controls.rtfEditor.OnProcess(DoExit);
            this.helpEd.SetMenuEnableEXIT(true);
        }

        public string myHelpCode = null;
        private string lastCode = null;
        data.masterDS.sysHelpDataTable helpTbl = new data.masterDS.sysHelpDataTable();
        public void ShowForm()
        {
            this.helpEd.SetReadOnly(this.myPermission.Admin == false);
            if (this.lastCode != this.myHelpCode && this.myHelpCode != null)
            {
                this.helpTbl.Clear();
                DataAccess.DataLibs.LoadData(this.helpTbl, this.myHelpCode);
                try
                {
                    if (this.helpTbl.Count > 0) this.helpEd.myText = this.helpTbl[0].helpText;
                }
                catch { }
                this.lastCode = this.myHelpCode;
            }
            this.ShowDialog();
        }

        private bool DoSaveHelp(object sender)
        {
            if (this.myHelpCode == null) return false;
            data.masterDS.sysHelpRow row = this.helpTbl.FindBycode(this.myHelpCode);
            if (row == null)
            {
                row = this.helpTbl.NewsysHelpRow();
                DataAccess.DataLibs.InitData(row);
                row.code = this.myHelpCode;
                this.helpTbl.AddsysHelpRow(row);
            }
            row.helpText = helpEd.myText;
            DataAccess.DataLibs.UpdateData(row);
            this.ShowMessage("Đã lưu trợ giúp");
            return true;
        }

        private bool DoExit(object sender)
        {
            this.Close();
            return true;
        }
    }
}
